#set security code
li $1, 2

$CheckCode$
#display 0000 on the LED
li $2, 0
sw $2, $0(259)
#check for pb1 button pressed
lw $3, 100000001
lw $3, $4(0)
li $5, 1
bne $4, $5($CheckCode$)
#load code in the switches, --reuse $2, $3--	
li $2, 100000000
lw $2, $3(0)
bne $1, $3($CheckCode$)

$CorrectCode$
#display 1111 on the LED
li $6, 15
sw $6, $0(259)		
#100 loops equals 10 seconds on processor speed ***TODO***
li $7, 100						


$Loop$
#decrement loop counter
li $8, 1
sub $7, $7, $8					
#go to CheckCode if time is out
beq $7, $0($CheckCode$)
#go to Loop if pb2 is not pressed
li $9, 100000010
li $10, 1
bne $9, $10($Loop$)			

$Balance$
#increment number of coins
add $11, $11, $10					
#display number of coins
sw $11, $0(259)				
#100 loops equals 10 seconds on processor speed ***TODO***
li $12, 100						

$Loop1$
#decrement loop counter
sub $12, $12, $10					
#go to CheckCode if time is out
beq $12, $0($CheckCode$)
#go to Loop1 if neither pb1 or pb2 was pressed --reuse $4, $5, $6 --
li $13, 100000001 
li $14, 100000010
lw $13, $15(0)
lw $14, $4(0)
or $5, $15, $4  	
bne $6, $10($Loop1$)
#go to Balance if coin is added(pb2 was pressed) else (pb1 was pressed) --reuse $7, $8, $9 --
lw $14, $7(0)
beq $7, $10($Balance$)
#check for enough coins
li $9, 3
slt $8, $11, $9					
#go to Balance if not enough money
beq $8, $10($Balance$)

$Deliver$
#100 loops equals 10 seconds on processor speed --reuse $12 --***TODO***
li $12, 100					
#substract 3 coins if enough money	
sub $11, $11, $9					

$Loop2$
#decrement loop counter
sub $12, $12, $10					
#go to CheckCode if time is out
beq $12, $0($CheckCode$)
#go to Loop3 if pb2 is not pressed
bne $14, $10($Loop3$)			
#go to CorrectCode if pb2 is pressed 
beq $14, $10($CorrectCode$)	
